import type { String } from "lodash";
import { api } from "../api";
import type { SubStateCard } from "./types/MicroSceneTypes";
// 获取子阶段ID列表
export const getSubstateIdList = async (sceneId: number, phaseId: number) => {
  try {
    const response = await api.get(`/java/ground/aix/sub_phase/get_sub_phases?sceneId=${sceneId}&phaseId=${phaseId}`)
    return response.data
  }
  catch (error) {
    console.error("获取子阶段ID列表失败", error);
    throw new Error("获取子阶段ID列表失败");
  }
}

// 添加子阶段
export const addSubstate = async (sceneId: number, phaseId: number) => {
  try {
    const response = await api.post(`/java/ground/aix/sub_phase/insert_sub_phase?sceneId=${sceneId}&phaseId=${phaseId}`)
    return response.data
  }
  catch (error) {
    console.error("添加子阶段失败", error);
    throw new Error("添加子阶段失败");
  }
}

// 删除子阶段
export const deleteSubstate = async (sceneId: number, subPhaseId: number) => {
  try {
    const response = await api.post(`/java/ground/aix/sub_phase/delete_sub_phase?sceneId=${sceneId}&subPhaseId=${subPhaseId}`)
    return response.data
  }
  catch (error) {
    console.error("删除子阶段失败", error);
    throw new Error("删除子阶段失败");
  }
}

// 获取子阶段基本信息
export const getSubstateInfo = async (sceneId: number, subPhaseId: number) => {
  try {
    const response = await api.get(`/java/ground/aix/sub_phase/get_sub_phase_info?sceneId=${sceneId}&subPhaseId=${subPhaseId}`)
    return response.data
  }
  catch (error) {
    console.error("获取子阶段基本信息失败", error);
    throw new Error("获取子阶段基本信息失败");
  }
}

// 修改子阶段基本信息
export const updateSubstateInfo = async (subStateCard: SubStateCard) => {
  try {
    const response = await api.post(`/java/ground/aix/sub_phase/update_sub_phase`, subStateCard)
    return response.data
  }
  catch (error) {
    console.error("修改子阶段基本信息失败", error);
    throw new Error("修改子阶段基本信息失败");
  }
}

// 获取子阶段所有资源
export const getSubstateResources = async (sceneId: number, subPhaseId: number) => {
  try {
    const response = await api.get(`/java/ground/aix/sub_phase/get_sub_phase_resources?sceneId=${sceneId}&subPhaseId=${subPhaseId}`)
    return response.data
  }
  catch (error) {
    console.error("获取子阶段所有资源失败", error);
    throw new Error("获取子阶段所有资源失败");
  }
}

// 添加子阶段资源
export const addSubstateResource = async (
  resourceTitle: string,
  introduction: string,
  subPhaseId: string,
  targetUrl: string,
  tags: string,
  sceneId: string,
  coverImage: File,
) => {
  try {
    const formData = new FormData()
    // 添加文件
    formData.append('coverImage', coverImage)
    // 添加其他参数
    formData.append('resourceTitle', resourceTitle)
    formData.append('introduction', introduction)
    formData.append('subPhaseId', subPhaseId)
    formData.append('targetUrl', targetUrl)
    formData.append('tags', tags)
    formData.append('sceneId', sceneId)

    const response = await api.post(
      '/java/ground/aix/sub_phase/insert_sub_phase_resource',
      formData,
      {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      },
    )
    return response.data
  }
  catch (error) {
    console.error("添加子阶段资源失败", error);
    throw new Error("添加子阶段资源失败");
  }
}

// 删除子阶段资源
export const deleteSubstateResource = async (sceneId: number, resourceId: number) => {
  try {
    const response = await api.post(`/java/ground/aix/sub_phase/delete_sub_phase_resource?sceneId=${sceneId}&resourceId=${resourceId}`)
    return response.data
  }
  catch (error) {
    console.error("删除子阶段资源失败", error);
    throw new Error("删除子阶段资源失败");
  }
}

// 修改子阶段资源基本信息
export const updateSubstateResourceInfo = async (sceneId: number, resourceId: number, resourceTitle: string, introduction: string, subPhaseId: number, targetUrl: string, tags: string[]) => {
  try {
    const response = await api.post(`/java/ground/aix/sub_phase/update_sub_phase_resource`, { sceneId, resourceId, resourceTitle, introduction, subPhaseId, targetUrl, tags })
    return response.data
  }
  catch (error) {
    console.error("修改子阶段资源基本信息失败", error);
    throw new Error("修改子阶段资源基本信息失败");
  }
}

// 修改子阶段资源封面图片
export const updateSubstateResourceCoverImage = async (sceneId: number, resourceId: number, coverImage: File) => {
  try {
    const formData = new FormData()
    formData.append('coverImage', coverImage)
    formData.append('sceneId', sceneId.toString())
    formData.append('resourceId', resourceId.toString())
    const response = await api.post(`/java/ground/aix/sub_phase/update_sub_phase_resource_cover`, formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    })
    return response.data
  }
  catch (error) {
    console.error("修改子阶段资源封面图片失败", error);
    throw new Error("修改子阶段资源封面图片失败");
  }
}
